package com.ruoyi.well.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.well.domain.MonthlyWellData;
import com.ruoyi.well.domain.WellData;
import com.ruoyi.well.domain.YearlyWellData;
import com.ruoyi.well.service.WellService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api("机井数据处理")
@Slf4j
@RestController
@RequestMapping("/well")
public class WellController {


    @Autowired
    private WellService wellService;

    @ApiOperation("区域用水-每个设备每年的用水量")
    @GetMapping("/areaYearData")
    public AjaxResult areaYearData(Integer areaId,String startYear,String endYear){
        List<String> deviceIdList = wellService.selectDeviceListByAreaId(areaId);
        String deviceIdString = String.join(",", deviceIdList);
        return AjaxResult.success(wellService.waterDataStatisticsYear(deviceIdString, startYear, endYear));
    }

    @ApiOperation("返回每年的用水用电量")
    @GetMapping("/yearData")
    public AjaxResult yearData(String deviceIds,String startYear,String endYear){

        List<WellData> wellDataList = wellService.waterDataStatisticsYear(deviceIds, startYear, endYear);

        Map<String, Double> yearlyWaterConsumptionMap = new HashMap<>();
        Map<String, Double> yearlyElectricityConsumptionMap = new HashMap<>();

        // 计算每年的总用水量和总用电量
        for (WellData wellData : wellDataList) {
            String year = wellData.getDate();
            Double waterConsumption = wellData.getWaterVolYear();
            Double electricityConsumption = wellData.getElectricVolYear();

            if (yearlyWaterConsumptionMap.containsKey(year)) {
                double totalWaterConsumption = yearlyWaterConsumptionMap.get(year) + waterConsumption;
                yearlyWaterConsumptionMap.put(year, totalWaterConsumption);
            } else {
                yearlyWaterConsumptionMap.put(year, waterConsumption);
            }

            if (yearlyElectricityConsumptionMap.containsKey(year)) {
                double totalElectricityConsumption = yearlyElectricityConsumptionMap.get(year) + electricityConsumption;
                yearlyElectricityConsumptionMap.put(year, totalElectricityConsumption);
            } else {
                yearlyElectricityConsumptionMap.put(year, electricityConsumption);
            }
        }

        // 构建返回的数据结构
        List<YearlyWellData> result = new ArrayList<>();
        for (Map.Entry<String, Double> entry : yearlyWaterConsumptionMap.entrySet()) {
            double waterConsumption = entry.getValue();
            double electricityConsumption = yearlyElectricityConsumptionMap.getOrDefault(entry.getKey(), 0.0);
            result.add(new YearlyWellData(entry.getKey(), waterConsumption, electricityConsumption));
        }
        return AjaxResult.success(result);
    }

    @ApiOperation("一年内每月所有设备用水量")
    @GetMapping("/monthData")
    public AjaxResult monthData(String deviceIds,String year){
        List<WellData> wellDataList = wellService.waterDataStatisticsMonth(deviceIds, year);
        List<MonthlyWellData> monthlyWellDataList = new ArrayList<>();
        for(WellData wellData:wellDataList){
            Double electricVolMonth = wellData.getElectricVolMonth();
            Double waterVolMonth = wellData.getWaterVolMonth();
            String month = wellData.getDate();
            MonthlyWellData monthlyWellData = new MonthlyWellData().setMonth(month).setWaterVolMonth(waterVolMonth).setElectricVolMonth(electricVolMonth);
            monthlyWellDataList.add(monthlyWellData);
        }
        return AjaxResult.success(monthlyWellDataList);
    }

    @ApiOperation("一年内每设备每日用水量")
    @GetMapping("/dayData")
    public AjaxResult dayData(String deviceIds,String year){
        List<WellData> wellDataList = wellService.waterDataStatisticsDay(deviceIds, year);
        return AjaxResult.success(wellDataList);
    }

}
